package com.weizhu.swufer.core.filter;

import com.weizhu.swufer.constant.SwuferConstant;
import com.weizhu.swufer.core.annotation.SwuferFilter;
import com.weizhu.swufer.core.entity.InvokeParam;
import com.weizhu.swufer.core.rpc.RpcContext;
import com.weizhu.swufer.core.rpc.RpcRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SwuferFilter(type = {SwuferConstant.FILTER_TYPE_CLIENT,SwuferConstant.FILTER_TYPE_PROVIDER},order = 0)
public class LogFilter implements Filter{
    private final static Logger logger = LoggerFactory.getLogger(LogFilter.class);
    @Override
    public Object doFilter(InvokeParam invokeParam, FilterChain filterChain) throws Exception {

        long start = System.currentTimeMillis();
        Object o = filterChain.doFilter(invokeParam);
        long cost  = System.currentTimeMillis() - start;
        logger.info("trace-id:" + RpcContext.getContext().getParameter(SwuferConstant.TRACE_ID)
                + ", remote call success,cost:"+ cost + "ms");
        return o;
    }
}
